package com.gitee.feizns.quickstart.jackson.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * 将任意单个json节点使用数组包装（作为一个仅包含单个元素的json数组）
 * <pre>{@code
 *  //如果原json是 "feizns"             -> "["feizns"]"
 *  //如果原json是 "{"name":"feizns"}"  -> "[{"name":"feizns"}]"
 *  //如果原json是 "["name"]"           -> "[["name"]]"
 * }</pre>
 * @author feizns
 * @since 2023/4/2
 */
public class ToSingletonArraySerializer extends JsonSerializer<Object> {

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartArray();
        gen.writePOJO(value);
        gen.writeEndArray();
    }

}
